n = int(input())
a1,b1,c1 = map(int,input().split())
a2,b2,c2 = map(int,input().split())

win = min(a1,b2) + min(b1,c2) + min(c1,a2)
a1_left = a1 - min(a1,b2)
a2_left = a2 - min(c1,a2)
b1_left = b1 - min(b1,c2)
b2_left = b2 - min(b2,a1)
c1_left = c1 - min(c1,a2)
c2_left = c1 - min(c2,b1)
equal = min(a1_left,a2_left) + min(b1_left,b2_left) + min(c1_left,c2_left)
print(f'{win*2+equal}')